package com.ogawa.projectcommon.dboperation;

import androidx.annotation.NonNull;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.ogawa.base.base.BaseApp;
import com.ogawa.base.constants.Constants;
import com.ogawa.projectcommon.appdatabase.AppDatabase;
import com.ogawa.projectcommon.bean.EntityUserDevice;
import com.ogawa.projectcommon.bean.Program8506E;
import com.ogawa.projectcommon.dao.EntityUserDeviceDao;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据库
 */
public class OperationDB {
    private AppDatabase db;

    public AppDatabase getDb() {
        if (db == null) {
            synchronized (this) {
                if (db == null) {
                    db = Room.databaseBuilder(BaseApp.Companion.getAppContext(),
                            AppDatabase.class, "AppDatabase")
                            .allowMainThreadQueries()//允许在主线程中查询,如果没有这句，就只能异步调用
                            .addCallback(new RoomDatabase.Callback() {
                                @Override
                                public void onCreate(@NonNull SupportSQLiteDatabase db) {
                                    super.onCreate(db);
                                    init8506EProgram();
                                }
                            })
                            .addMigrations(MIGRATION_1_2)
                            .build();
                }
            }
        }
        return db;
    }

    private final Migration MIGRATION_1_2 = new Migration(1, 2) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            //将数据表创建出来
            database.execSQL("CREATE TABLE 'collect' ('id'  INTEGER NOT NULL,'programId' INTEGER NOT NULL,'program' TEXT,'programName' TEXT,'typeCode' TEXT,'index' INTEGER  NOT NULL,'type' INTEGER NOT NULL,PRIMARY KEY ('id')) ");
            database.execSQL("CREATE TABLE 'program8506E' ('id'  INTEGER  NOT NULL,'type' INTEGER  NOT NULL,'program' TEXT,'imageName' TEXT,PRIMARY KEY ('id')) ");
            database.execSQL("CREATE TABLE 'diy' ('id'  INTEGER  NOT NULL,'typeCode' TEXT,'diyName' TEXT,'content' TEXT,'appIndex' INTEGER NOT NULL,PRIMARY KEY ('id')) ");
            init8506EProgram();
        }
    };


    private synchronized void init8506EProgram() {
        List<Program8506E> beans = new ArrayList<>();
        Program8506E bean = new Program8506E("1", 1, "selector_dashi");
        Program8506E bean1 = new Program8506E("2", 1, "selector_taishi");
        Program8506E bean2 = new Program8506E("3", 1, "selector_guangjie");
        Program8506E bean3 = new Program8506E("4", 1, "selector_shujin");
        Program8506E bean4 = new Program8506E("5", 1, "selector_yuanqi");
        Program8506E bean5 = new Program8506E("6", 1, "selector_pinghen");
        Program8506E bean6 = new Program8506E("7", 1, "selector_shenceng");
        Program8506E bean7 = new Program8506E("8", 1, "selector_qingchen");
        Program8506E bean8 = new Program8506E("9", 1, "selector_huoli");
        Program8506E bean9 = new Program8506E("10", 1, "selector_taiji");
        Program8506E bean10 = new Program8506E("11", 1, "selector_nvwang");
        Program8506E bean11 = new Program8506E("12", 1, "selector_nanshi");
        Program8506E bean12 = new Program8506E("13", 1, "selector_laonian");
        Program8506E bean13 = new Program8506E("14", 1, "selector_japanese");
        Program8506E bean14 = new Program8506E("15", 1, "selector_indian");
        Program8506E bean15 = new Program8506E("16", 1, "selector_paris");
        Program8506E bean16 = new Program8506E("17", 1, "selector_chinese");
        Program8506E bean17 = new Program8506E("18", 1, "selector_bangong");
        Program8506E bean18 = new Program8506E("19", 1, "selector_ditou");
        Program8506E bean19 = new Program8506E("20", 1, "selector_jiache");
        Program8506E bean20 = new Program8506E("21", 1, "selector_gaogen");
        Program8506E bean21 = new Program8506E("22", 1, "selector_jiuyang");
        Program8506E bean22 = new Program8506E("23", 1, "selector_xianglong");
        Program8506E bean23 = new Program8506E("24", 1, "selector_liumai");
        Program8506E bean24 = new Program8506E("25", 1, "selector_yiyang");
        Program8506E bean25 = new Program8506E("26", 1, "selector_taijiquan");
        Program8506E bean26 = new Program8506E("27", 1, "selector_qiankun");
        Program8506E bean27 = new Program8506E("1", 6, "selector_ball");
        Program8506E bean28 = new Program8506E("2", 6, "selector_golf");
        Program8506E bean29 = new Program8506E("3", 6, "selector_swimming");
        Program8506E bean30 = new Program8506E("4", 6, "selector_jianzou");
        Program8506E bean31 = new Program8506E("5", 6, "selector_riding");
        Program8506E bean32 = new Program8506E("6", 6, "selector_jianshen");
        Program8506E bean33 = new Program8506E("7", 6, "selector_running");
        beans.add(bean);
        beans.add(bean1);
        beans.add(bean2);
        beans.add(bean3);
        beans.add(bean4);
        beans.add(bean5);
        beans.add(bean6);
        beans.add(bean7);
        beans.add(bean8);
        beans.add(bean9);
        beans.add(bean10);
        beans.add(bean11);
        beans.add(bean12);
        beans.add(bean13);
        beans.add(bean14);
        beans.add(bean15);
        beans.add(bean16);
        beans.add(bean17);
        beans.add(bean18);
        beans.add(bean19);
        beans.add(bean20);
        beans.add(bean21);
        beans.add(bean22);
        beans.add(bean23);
        beans.add(bean24);
        beans.add(bean25);
        beans.add(bean26);
        beans.add(bean27);
        beans.add(bean28);
        beans.add(bean29);
        beans.add(bean30);
        beans.add(bean31);
        beans.add(bean32);
        beans.add(bean33);
        new Thread(() -> {
            getDb().getProgram8506EDao().insertItems(beans);
        }).start();
    }

    /**
     * 添加设备
     *
     * @param device
     * @return
     */
    public void addEntityUserDevice(EntityUserDevice device) {
        EntityUserDeviceDao dao = getDb().entityUserDeviceDao();
        dao.insertUserDevice(device);
    }

    /**
     * 查找设备
     *
     * @param name
     * @return
     */
    public EntityUserDevice[] queryEntityUserDeviceByName(String name) {
        EntityUserDeviceDao dao = getDb().entityUserDeviceDao();
        return dao.loadAllEntityUserDeviceByName(name);
    }

    /**
     * 根据sn查找设备
     *
     * @param sn
     * @return
     */
    public EntityUserDevice queryEntityUserDeviceBySn(String sn) {
        EntityUserDeviceDao dao = getDb().entityUserDeviceDao();
        EntityUserDevice entityUserDevices = dao.loadAllEntityUserDeviceBySN(sn);
        return entityUserDevices;
    }

    /**
     * 根据deviceId查找设备
     *
     * @param deviceId
     * @return
     */
    public EntityUserDevice queryEntityUserDeviceByDeviceID(String deviceId) {
        EntityUserDeviceDao dao = getDb().entityUserDeviceDao();
        EntityUserDevice entityUserDevices = dao.loadAllEntityUserDeviceByDeviceID(deviceId);
        return entityUserDevices;
    }

    /**
     * 根据sn删除设备
     *
     * @param sn
     * @return
     */
    public void deleteEntityUserDeviceBySn(String sn) {
        EntityUserDeviceDao dao = getDb().entityUserDeviceDao();
        EntityUserDevice entityUserDevices = dao.loadAllEntityUserDeviceBySN(sn);
        if (entityUserDevices != null) {
            dao.deleteEntityUserDevice(entityUserDevices);
        }
    }


    /**
     * 修改设备
     *
     * @param entityUserDevice
     * @return
     */
    public void updateEntityUserDevice(EntityUserDevice entityUserDevice) {
        EntityUserDeviceDao dao = getDb().entityUserDeviceDao();
        dao.updateEntityUserDevice(entityUserDevice);
    }


}
